#include "cameraNumSetting.h"

CameraNumSetting::CameraNumSetting(QWidget *parent) : MiddleWidgetVirtual(parent)
{
    exposureText = new QLabel();
    exposureTimerUnitsText = new QLabel("(ms)");
    exposureSpinBox = new QSpinBox();
    exposureSpinBox->setRange(exposureMinnum, exposureRange);
    exposureSpinBox->setFixedSize(60, 25);
    exposureComboBox = new QComboBox();
    exposureComboBox->setFixedSize(90, 25);
    exposureComboBox->addItem(QString("1~100ms"));
    exposureComboBox->addItem(QString("1~200ms"));
    exposureComboBox->addItem(QString("1~400ms"));
    exposureComboBox->addItem(QString("1~800ms"));
    connect(exposureComboBox, &QComboBox::currentTextChanged, this, &CameraNumSetting::exposureRangeChange);

    exposureSlider = new DoubleSlider();
    exposureSlider->setRange(double(exposureMinnum), double(exposureMaxnum));
    connect(exposureSlider, &DoubleSlider::valueChanged, this, [=](double value){
       senderValue(signalKind::EXPOSURE, int(value));
       exposureSpinBox->setValue(int(value));
    });
     // valueChanged有重载的方法所以需要指明是哪一个重载的方法(需要传入参数类型)
    connect(exposureSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [=](int value){
        exposureSlider->setValue(value);
        senderValue(signalKind::EXPOSURE, value);
    });

    exposureAuto = new QCheckBox();
    exposureAuto->setFixedSize(60, 25);

    //增益区域
    bufferText = new QLabel();
    bufferSpinBox = new QSpinBox();
    bufferSpinBox->setRange(1, 100);
    bufferSpinBox->setFixedSize(60, 25);
    QLabel *placeLabel = new QLabel();

    bufferSlider = new DoubleSlider();
    bufferSlider->setRange(double(exposureMinnum), double(exposureMaxnum));
    connect(bufferSlider, &DoubleSlider::valueChanged, this, [=](double value){
       senderValue(signalKind::BUFFER, int(value));
       bufferSpinBox->setValue(int(value));
    });

    connect(bufferSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, [=](int value){
        bufferSlider->setValue(value);
        senderValue(signalKind::BUFFER, value);
    });

    bufferAuto = new QCheckBox();
    bufferAuto->setFixedSize(60, 25);
    moreBtn = new QPushButton();
    moreBtn->setFixedSize(50, 35);
    HLayout1 = new QHBoxLayout();
    HLayout2 = new QHBoxLayout();
    HLayout3 = new QHBoxLayout();
    HLayout4 = new QHBoxLayout();
    VLayout  = new QVBoxLayout();

    //将控件添加到对应的布局中
    HLayout1->addWidget(exposureText);
    HLayout1->addWidget(exposureSpinBox);
    HLayout1->addWidget(exposureTimerUnitsText);
    HLayout1->addWidget(exposureComboBox);

    HLayout2->addWidget(exposureSlider);
    HLayout2->addWidget(exposureAuto);

    HLayout3->addWidget(bufferText);
    HLayout3->addWidget(bufferSpinBox);
    HLayout3->addWidget(placeLabel);

    HLayout4->addWidget(bufferSlider);
    HLayout4->addWidget(bufferAuto);
    HLayout4->addWidget(moreBtn);
    HLayout4->setAlignment(Qt::AlignVCenter);

    VLayout->addLayout(HLayout1);
    VLayout->addLayout(HLayout2);
    VLayout->addLayout(HLayout3);
    VLayout->addLayout(HLayout4);
    VLayout->setMargin(0);

    this->setLayout(VLayout);
    this->setFixedSize(QSize(300, FixedHeight));

    //进行QSS样式处理
    this->setStyleSheet(QSS::QComboBox);
    this->setStyleSheet(QSS::QSpinBox);
    this->setStyleSheet(QSS::QLabel);
    this->setStyleSheet(QSS::QCheckBox);
    LanguageInit();
}

int CameraNumSetting::RFixedHeight()
{
    return FixedHeight;
}

int CameraNumSetting::fixedWidth(ParameterKind kind, int widget)
{
    if(!widget)
    ELOG("中心框架尝试修改错误值0");
    if(kind == ParameterKind::Set && widget != 0)
    {
        FixedWidth = widget;
        this->setFixedWidth(FixedWidth);
    }
    return FixedWidth;
}

bool CameraNumSetting::setPaletteBF(const QPalette& palette)
{
    this->setPalette(palette);
    this->setAutoFillBackground(true);
    //遍历子组件然后更新他们的调色板
    foreach (QObject *child, this->children())
    {
        if(QWidget* childWidget = qobject_cast<QWidget*>(child))
        {
            childWidget->setPalette(palette);
            this->setAutoFillBackground(true);
        }
    }
    return true;
}

void CameraNumSetting::LanguageInit()
{
    exposureText->setText(tr("曝光"));
    exposureAuto->setText(tr("自动"));
    bufferText->setText(tr("增益"));
    bufferAuto->setText(tr("自动"));
    moreBtn->setToolTip(tr("更多设置"));
}

void CameraNumSetting::exposureRangeChange(const QString& exposureRangeText)
{
    QString tmpText = exposureRangeText;
    tmpText.remove(0, 2);
    exposureRange = tmpText.toUInt();
    exposureSpinBox->setRange(exposureMinnum, exposureRange);
}
